package com.administrator.customviewtest.view_group;

import android.content.Context;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2016/8/10.
 */
public class TestViewGroup2 extends ViewGroup {

    public TestViewGroup2(Context context) {
        this(context, null);
    }

    public TestViewGroup2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestViewGroup2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

        //根据子view的高度和宽度计算自己的高度和宽度
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        //左边的高度右边的高度 对比取最大值，顶部的宽度，底部的宽度取最大值。。。
        int lHeight = 0, rHeight = 0, tWidth = 0, bWidth = 0;

//        int cCount = getChildCount();
//        for (int i = 0; i < cCount; i++) {
//            View childView = getChildAt(i);
//            int cWidth = childView.getMeasuredWidth();
//            int cHeight = childView.getMeasuredHeight();
//            MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams();
//
//            // 上面两个childView的宽度
//            if (i == 0 || i == 1) {
//                tWidth += params.leftMargin + params.rightMargin + cWidth;
//            }
//            //下面两个childView的宽度
//            if (i == 2 || i == 3) {
//                bWidth += params.leftMargin + params.rightMargin + cWidth;
//            }
//            //左面两个view的高度和
//            if (i == 0 || i == 2) {
//                lHeight = params.topMargin + params.bottomMargin + cHeight;
//            }
//
//            //右面两个view的高度和
//            if (i == 0 || i == 2) {
//                rHeight = params.topMargin + params.bottomMargin + cHeight;
//            }
//
//        }

        View childView0 = getChildAt(0);
        int cWidth = childView0.getMeasuredWidth();
        int cHeight = childView0.getMeasuredHeight();
        MarginLayoutParams params = (MarginLayoutParams) childView0.getLayoutParams();

        int height = getChildAt(0).getMeasuredHeight() + ((MarginLayoutParams) (getChildAt(0).getLayoutParams())).topMargin +
                     getChildAt(1).getMeasuredHeight() + ((MarginLayoutParams) (getChildAt(0).getLayoutParams())).topMargin +
                getChildAt(3).getMeasuredHeight() + ((MarginLayoutParams) (getChildAt(3).getLayoutParams())).bottomMargin;


        int width = getChildAt(1).getMeasuredWidth() + ((MarginLayoutParams) (getChildAt(1).getLayoutParams())).leftMargin +
                getChildAt(2).getMeasuredWidth() + ((MarginLayoutParams) (getChildAt(2).getLayoutParams())).rightMargin+
                getChildAt(3).getMeasuredWidth() + ((MarginLayoutParams) (getChildAt(2).getLayoutParams())).rightMargin;


        setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? sizeWidth : width, heightMode == MeasureSpec.EXACTLY ? sizeHeight : height);

    }


    @Override
    protected void onLayout(boolean changeStatus, int l, int t, int r, int b) {

        //确定子view的位置
        int cCount = getChildCount();
        for (int i = 0; i < cCount; i++) {
            View childView = getChildAt(i);
            int width = childView.getMeasuredWidth();
            int height = childView.getMeasuredHeight();
            MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams();
            int cLeft = 0;
            int cRight = 0;
            int cTop = 0;
            int cBottom = 0;

            switch (i) {
                case 0: {
                    cLeft = getWidth() / 2 - width / 2;
                    cTop = params.topMargin;
                    break;
                }
                case 1: {
                    cLeft = params.leftMargin;
                    cTop = getHeight() / 2 - height / 2;
                    break;
                }
                case 2: {
                    cLeft = getWidth() - params.rightMargin - width;
                    cTop = getHeight() / 2 - height / 2;
                    break;
                }
                case 3: {
                    cLeft = getWidth() / 2 - width / 2;
                    cTop = getHeight() - params.bottomMargin - height;
                    break;
                }

            }


            cRight = cLeft + width;
            cBottom = cTop + height;
            childView.layout(cLeft, cTop, cRight, cBottom);


        }
    }
}
